package com.anlu.base.system.modern;

import com.anlu.base.system.AntiCorruptionLayer;
import com.anlu.base.system.ShopException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class ModernShop {

    @Autowired
    private ModernStore store;

    @Autowired
    private AntiCorruptionLayer acl;

    public void placeOrder(ModernOrder order)throws ShopException{

        String id = order.getId();
        Optional<ModernOrder> orderInObsoleteSystem = acl.findOrderInLegacySystem(id);

        if(orderInObsoleteSystem.isPresent()){
            var legacyOrder =orderInObsoleteSystem.get();
            if (!order.equals(legacyOrder)) {
                throw ShopException.throwIncorrectData(legacyOrder.toString(), order.toString());
            }
        }else{
            store.put(id,order);
        }

    }

    /**
     * Finds the order in the modern system.
     */
    public Optional<ModernOrder> findOrder(String orderId) {
        return store.get(orderId);
    }
}
